const path = require('path');
const htmlWebpackPlugin = require('html-webpack-plugin');
const {CleanWebpackPlugin} = require('clean-webpack-plugin');

module.exports = {
    entry: [
        './src/main.js',
        "core-js/modules/es6.promise",
        "core-js/modules/es6.array.iterator",
    ], //入口文件(要构建的文件)
    output: {
        filename: 'bundle.js', //构建输出文件名
        path: path.resolve(__dirname, 'dist') //构建输出文件夹
    },
    devServer: {  //web-dev-server配置
        open: true,
        port: 3000,
        // contentBase: path.join(__dirname, "dist"),
        hot: true,
        // proxy: {
        //     '/api': {
        //         target: 'http://vue.studyit.io',
        //         changeOrigin: true,
        //     }
        // }
    },
    plugins: [
        new htmlWebpackPlugin({ //自动追加 <script type="text/javascript" src="bundle.js"></script></body>到页面最后
            template: path.join(__dirname, './src/index.html'),//指定模板页面,
            filename: "index.html",//生成的页面名称
            title: "react"
        }),
        new CleanWebpackPlugin(), //打包前清理dist目录
    ],
    module: {
        rules: [
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader' //添加css模块化
                ],
            },
            {
                test: /\.scss$/,
                use: [
                    'style-loader',
                    'css-loader?modules',
                    'sass-loader',
                ]
            },
            {
                test: /\.(png|svg|jpg|gif)$/,
                use: [
                    'file-loader'
                ]
            },
            {
                test: /\.(woff|woff2|eot|ttf|otf)$/,
                use: [
                    'file-loader'
                ]
            },
            {
                test: /\.(js|jsx)$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env','@babel/react'],
                        plugins: [
                            require('@babel/plugin-proposal-class-properties'),
                            ["import", {
                                "libraryName": "antd",
                                "libraryDirectory": "es",
                                "style": "css" // `style: true` 会加载 less 文件
                            }]
                        ]
                    }
                },
                exclude: "/node_modules", //排除node_modules
            }
        ]
    },
    resolve: {
        //修改vue导入时的路径
        alias: {
        }
    }
};
